<?php
// +----------------------------------------------------------------------
// | YikeCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://zhuizhan.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: Doo <yws_sky@163.com> | <Qq:1151414552>
// +----------------------------------------------------------------------
namespace Home\Controller;
use Org\Yike\Category;
use Think\Controller;
class CommonController extends Controller {
    public function _initialize(){
        //判断是否手机登录
        if($this->is_mobile()){
            Header("Location: ".d_name('m'));
            die;
        }
        //用户信息
        $this->user = getLoginUser();
        $top_menu = getLinkMenuFor(array(2,3,22,27),1);
        $top_menu['ask_cate'] = M('ask_cate')->where(array('status'=>1,'pid'=>0))->order('ordid desc')->select();
        $top_menu['article_cate'] = M('article_cate')->where(array('status'=>1,'pid'=>0))->order('ordid desc')->select();
        $this->top_menu = $top_menu;
        $this->contrller = strtolower(CONTROLLER_NAME);

       //网站配置
        $where = "name='site' or name='contact' or name='seo' or name='nav' or name='logo'";
        $setting = $setting = M('setting')->where($where)->select();
        $arr = array();
        foreach($setting as $s){
            $arr[$s['name']] = json_decode($s['data'],true);
        }
        $this->siteName         = $arr['site']['title'];
        $this->icp              = $arr['site']['icp'];
        $this->kefuphone        = $arr['contact']['telphone'];
        $this->site         = $arr['site'];
        $this->contact = $arr['contact'];
        $this->logo = $arr['logo'];
        $this->site_nav = $arr['nav'];
        $this->ak = 'ETLXgCxIoVixggHcAk6mKpMd';
        //关键词设置
        $this->seoKey(array(
            'seo_title'=>$arr['seo'][$this->contrller]['seo_title'],
            'seo_keys'=>$arr['seo'][$this->contrller]['seo_keys'],
            'seo_desc'=>$arr['seo'][$this->contrller]['seo_desc'],
        ),'name','',0);
   }
    
	
	public function myError(){
        send_http_status(404);
        $this->display('Public:404');
    }

    //判断是否手机登录
    public function is_mobile() {
        $mobile = array();
        static $mobilebrowser_list ='Mobile|iPhone|Android|WAP|NetFront|JAVA|OperasMini|UCWEB|WindowssCE|Symbian|Series|webOS|SonyEricsson|Sony|BlackBerry|Cellphone|dopod|Nokia|samsung|PalmSource|Xphone|Xda|Smartphone|PIEPlus|MEIZU|MIDP|CLDC';
        // 获取手机浏览器
        if(preg_match("/$mobilebrowser_list/i", $_SERVER['HTTP_USER_AGENT'], $mobile)) {
            return true;
        }else{
            if(preg_match('/(mozilla|chrome|safari|opera|m3gate|winwap|openwave)/i', $_SERVER['HTTP_USER_AGENT'])) {
                return false;
            }else{
                if($_GET['mobile'] === 'yes') {
                    return true;
                }else{
                    return false;
                }
            }
        }
    }

    public function seoKey($seo,$title='title',$end='',$site=1){
        $seo_title=$seo['seo_title'] ? $seo['seo_title'] : $seo[$title];
        $seo_keys=$seo['seo_keys'] ? $seo['seo_keys'] : $seo_title;
        if($end){
            $seo_title=$seo_title.$end;
            $seo_keys=$seo_keys.$end;
        }
        if($site){
            $siteName = '_'.$this->siteName;
        }
        $this->seo=array(
            'title'=>$seo_title.$siteName,
            'keys'=>$seo_keys,
            'desc'=>$seo['seo_desc']
        );
    }

    protected function ajaxReturnDialog($status=1, $msg='', $data='', $dialog='') {
        parent::ajaxReturn(array(
            'status' => $status,
            'msg' => $msg,
            'data' => $data,
            'dialog' => $dialog,
        ));
    }

}